package com.walterweimod.auxiliary_attributes.mobeffect;

import com.google.common.base.CaseFormat;
import com.walterweimod.auxiliary_attributes.attribute.ModAttributes;
import com.walterweimod.auxiliary_attributes.capability.ModCapabilities;
import com.walterweimod.auxiliary_attributes.capability.impl.base.ChargeRangedValueBase;
import com.walterweimod.auxiliary_attributes.util.ModAttributeUtils;
import com.walterweimod.auxiliary_attributes.util.ModCapabilityUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeMap;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.UUID;

import static com.walterweimod.auxiliary_attributes.Auxiliary_attributes.getLogger;
import static com.walterweimod.auxiliary_attributes.util.ModAttributeUtils.getNameAttributeUUID;
import static com.walterweimod.auxiliary_attributes.util.ModCapabilityUtils.getCapabilityValue;
import static com.walterweimod.auxiliary_attributes.util.ModUtils.*;

public class Rage2 extends MobEffect {
    public static final String DESCRIPTION = "rage2";

    private static final UUID RAGE2_MIN_RAGE_UUID = getNameAttributeUUID(
            ModAttributes.MIN_RAGE.get(),
            DESCRIPTION
    );

    private static final UUID RAGE2_MAX_RAGE_UUID = getNameAttributeUUID(
            ModAttributes.MAX_RAGE.get(),
            DESCRIPTION
    );

    public static UUID getRage2MinRageUUID() {
        return RAGE2_MIN_RAGE_UUID;
    }

    public static UUID getRage2MaxRageUUID() {
        return RAGE2_MAX_RAGE_UUID;
    }

    protected Rage2() {
        super(MobEffectCategory.BENEFICIAL, 0xFF00FF00);
    }

    @Override
    public @NotNull Component getDisplayName() {
        return new TextComponent(
                CaseFormat.LOWER_UNDERSCORE.to(
                        CaseFormat.UPPER_CAMEL, DESCRIPTION
                )
        );
    }

    @Override
    public void addAttributeModifiers(@NotNull LivingEntity p_19478_,
                                      @NotNull AttributeMap p_19479_,
                                      int  p_19480_) {
        boolean canIncrease = getLivingEntityHealthPercent(p_19478_)
                <= Math.min(50.0F + 2.0F * p_19480_, 85.0F);
        float reducePercent = Math.min(50.0F + 2.0F * p_19480_, 85.0F)
                - getLivingEntityHealthPercent(p_19478_);
        double increaseValue = canIncrease ? Math.ceil(reducePercent / 4) : 0.0D;

        ModAttributeUtils.addAttributeModifier(
                p_19478_,
                ModAttributes.MIN_RAGE.get(),
                15.0D + 5.0 * p_19480_ + increaseValue,
                RAGE2_MIN_RAGE_UUID,
                AttributeModifier.Operation.ADDITION,
                false
        );

        ModAttributeUtils.addAttributeModifier(
                p_19478_,
                ModAttributes.MAX_RAGE.get(),
                10.0 *  p_19480_ + increaseValue,
                RAGE2_MAX_RAGE_UUID,
                AttributeModifier.Operation.ADDITION,
                false
        );

        ModCapabilityUtils.setCapabilityValue(
                p_19478_,
                ModCapabilities.RAGE_LAYER_VALUE,
                rageLayerValue -> {
                    rageLayerValue.setValue(0);
                    rageLayerValue.setLivingEntity(p_19478_);
                    rageLayerValue.setAttributeMap(Map.of(
                            "max", ModAttributes.MAX_RAGE.get(),
                            "min", ModAttributes.MIN_RAGE.get()
                    ));
                    rageLayerValue.addValue(15 + 5 * p_19480_ + ((int) increaseValue));
                }
        );

        if (!p_19478_.level.isClientSide()){
            getLogger().info("小战意初始化成功，战意值为：{}", getCapabilityValue(
                    p_19478_,
                    ModCapabilities.RAGE_LAYER_VALUE,
                    ChargeRangedValueBase::getValue,
                    0
            ));

            getLogger().info("小战意初始化成功，最小值为：{}", getCapabilityValue(
                    p_19478_,
                    ModCapabilities.RAGE_LAYER_VALUE,
                    ChargeRangedValueBase::getMinValue,
                    0
            ));

            getLogger().info("小战意初始化成功，最大值为：{}", getCapabilityValue(
                    p_19478_,
                    ModCapabilities.RAGE_LAYER_VALUE,
                    ChargeRangedValueBase::getMaxValue,
                    0
            ));
        }

        super.addAttributeModifiers(p_19478_, p_19479_,  p_19480_);
    }

    @Override
    public void removeAttributeModifiers(@NotNull LivingEntity p_19469_,
                                         @NotNull AttributeMap p_19470_,
                                         int p_19471_) {
        boolean canIncrease = getLivingEntityHealthPercent(p_19469_)
                <= Math.min(50.0F + 2.0F * p_19471_, 85.0F);
        float reducePercent = Math.min(50.0F + 2.0F * p_19471_, 85.0F)
                - getLivingEntityHealthPercent(p_19469_);

        ModAttributeUtils.removeAttributeModifier(
                p_19469_,
                ModAttributes.MIN_RAGE.get(),
                RAGE2_MIN_RAGE_UUID
        );

        ModAttributeUtils.removeAttributeModifier(
                p_19469_,
                ModAttributes.MAX_RAGE.get(),
                RAGE2_MAX_RAGE_UUID
        );

        ModCapabilityUtils.setCapabilityValue(
                p_19469_,
                ModCapabilities.RAGE_LAYER_VALUE,
                rageLayerValue ->
                        rageLayerValue.addValue(
                                -(15 + 5 * p_19471_
                                        + (int)(canIncrease ? Math.ceil(reducePercent / 4) : 0.0D))
                        )
        );

        if (!p_19469_.level.isClientSide()){
            getLogger().info("小战意移除成功");
        }

        super.removeAttributeModifiers(p_19469_, p_19470_, p_19471_);
    }
}
